/**
 * Created by Jie on 2017/2/3.
 */
var mysql = require('mysql');
var config = require('../conf/db');

var pool = mysql.createPool(config.mysql);

var result = null;

module.exports = {
    query:function (sql,callback) {
        pool.getConnection(function(err, connection) {
            if (err) {
                callback();
                return;
            }
            connection.query(sql, function(err, results) {
                if (err) {
                    callback(err, null);
                    return;
                }
                connection.release();
                callback(null, results);
            });
        });
    },
    queryFirst:function (sql,callback) {
        pool.getConnection(function(err, connection) {
            if (err) {
                callback();
                return;
            }
            connection.query(sql, function(err, results) {
                if (err) {
                    callback(err, null);
                    return;
                }
                connection.release();
                callback(null, results[0]);
            });
        });
    },
    exec:function (sql,callback) {
        pool.getConnection(function(err, connection) {
            if (err) {
                callback();
                return;
            }
            connection.query(sql, function(err, results) {
                if (err) {
                    callback(err, null);
                    return;
                }
                connection.release();
                callback(null, results);
            });
        });
    },
    trans:function (callback) {
        pool.getConnection(function(err, connection) {
            if (err) {
                callback(err,null);
                return;
            }
            callback(err,connection);
        });
    }
};

//insert 成功后的对象
var insertResult = {
    fieldCount: 0,
    affectedRows: 1,
    insertId: 16,
    serverStatus: 2,
    warningCount: 0,
    message: '',
    protocol41: true,
    changedRows: 0
}
